package cn.fxc;

import org.apache.commons.math3.stat.descriptive.moment.Mean;
import org.apache.commons.math3.stat.descriptive.moment.Variance;
import umontreal.ssj.gof.GofStat;
import umontreal.ssj.probdist.AndersonDarlingDist;
import umontreal.ssj.probdist.AndersonDarlingDistQuick;
import umontreal.ssj.probdist.NormalDist;

public class StatisticsExample2 {
    public void A_DTest(){

        double[] doubles = Util.loadData();
        double mean = new Mean().evaluate(doubles);
        System.out.println("均值: "+mean);
        double sd = Math.sqrt(new Variance().evaluate(doubles));
        System.out.println("标准差: "+sd);
        System.out.println("样本数: "+doubles.length);

        NormalDist normalDist = new NormalDist(mean,sd);
        double[] adStat = GofStat.andersonDarling(doubles, normalDist);
//        for (int i=0;i<adStat.length;i++)
        System.out.println("ad: "+ adStat[0]);
        double p = andersonDarlingNormalPValue(adStat[0], doubles.length);
        System.out.println("pValue: "+ p);

    }
    public static double andersonDarlingNormalPValue(double ADstar, int n) {
// 根据 Stephens (1974) 的近似公式
        if (ADstar < 0.2) {
            return 1 - Math.exp(-13.436 + 101.14 * ADstar - 223.73 * ADstar * ADstar);
        } else if (ADstar < 0.34) {
            return 1 - Math.exp(-8.318 + 42.796 * ADstar - 59.938 * ADstar * ADstar);
        } else if (ADstar < 0.6) {
            return Math.exp(0.9177 - 4.279 * ADstar - 1.38 * ADstar * ADstar);
        } else {
            return Math.exp(1.2937 - 5.709 * ADstar + 0.0186 * ADstar * ADstar);
        }
    }
}
